import { message } from 'ant-design-vue';
import { requestHttpDownload } from '@utils/index';
import { i18nRender } from '@/locales';

const downLoadFile = async (params) => {
  const { api } = params;
  if (!api) {
    message.error(i18nRender('gatewayVlanConf.downloadAddress'));
    return;
  }
  let data = {
    api: api,
    method: 'GET',
    module: 'default', //api模块，用于区分service 部署的ip,不传默认default，非/service/api/的模块都需要传该参数，巡检--inspection，日志--logbiz，终端管理--pyservice
    contentType: 'application/xlsx;charset=UTF-8',
    attachment: true, //是否以附件形式下载，默认true
    fileName:
      i18nRender('gatewayVlanConf.fileName') + `_${new Date().getTime()}.xlsx`,
    ...params,
  };
  // requestHttpDownload(data).then((ret) => {
  //   //真正下载
  //   let url = '/webproxy/common/file/download';
  //   let downloadData = ret.data.downloadData;
  //   url += '?downloadData=' + downloadData;
  //   window.open(url, '_self');
  // });
  const ret = await requestHttpDownload(data);
  let url = '/webproxy/common/file/download';
  let downloadData = ret.data.downloadData;
  url += '?downloadData=' + downloadData;
  return url; //返回url，然后使用a标签打开链接，不能使用window.open()开启下载，会偶发出现下载内容为空，可能是window.open 浏览器认为非用户行为会进行拦截
};

export { downLoadFile };
